/**
 * video music -> 音乐列表
 * @author tanggf
 */
$(function(){
	browse_index.run();
});

var browse_index = {
		run: function(){
			this._init_();
			this._start_();
			this._init_events();			
		},
		
		_init_: function(){
			this.contextPath = $("#contextPath").val() || "";
			this.period = $("#period").val() || "daily";			
			this.genre = $("#genre").val() || 'all';
			this.loadingHtml = "<div class='loading-box'><div class='loading-container'>Loading...</div></div>";
		},
		
		_start_: function(){
		},
		
		_init_events: function(){
			//vmenu
			$("#genre_select_div").vmenu({
				onchange: function(genre){
					browse_index.genre = genre;
					browse_index._select_musics();
				}
			});
			
			//tabs
			$("#browse_content_div").tabs({
				hoverClass: 'music-select-sort-span',
				onswitch: function($nav){
					browse_index.period = $nav.attr("period");
					browse_index._select_musics();
				}				
			});
			
			//href a
			this._bind_nav_a_click();
			this._bind_play_click();
		},
		
		_bind_nav_a_click: function($div){
			$div = $div || this._get_current_div();
			
			$div.find("div.pageing > a").click(function(){
				var href = $(this).attr("href") + "&ajax=1";
				if(href){
					browse_index._select_musics(href);
				}
				return false;
			});
		},
		
		_bind_play_click: function($div){
			$div = $div || this._get_current_div();
			$div.find("ul > li a").click(function(){
				var name = $(this).attr("name");
				_gaq.push(['_trackEvent', 'Browse_Music', 'Play', name]);		
			});
		},
		
		_select_musics: function(url){			
			url = url || this._get_select_url();
			var $div = this._get_current_div();
			$.ajax({
				url: url,
				beforeSend: function(){
					$div.html(browse_index.loadingHtml);
				},
				success: function(result){
					$div.html(result);
					browse_index._bind_nav_a_click($div);
				},
				error: function(){
					
				}
			});		
		},		
		
		_get_select_url: function(){
			var url = [this.contextPath, "/video/music/browses-", this.genre, '-1?period=', this.period, '&ajax=1'].join("");			
			return url;
		},
		
		_get_current_div: function(){
			return $("#" + this.period + "_music_div");
		}
};